import Mock from 'mockjs';
import sampleData from '../sampleData.json';
console.log('[Mock] seed mock loaded');

// Mock /api/seeds GET，根据 category 过滤
Mock.mock(/^\/api\/seeds(\?.*)?$/, 'get', (options) => {
  const url = new URL('http://dummy.com' + options.url); // 防止 URLSearchParams 报错
  const category = url.searchParams.get('category');

  const filtered = category
    ? sampleData.filter(item => item.type === category)
    : sampleData;

  return {
    code: 200,
    data: filtered
  };
});

// Mock /api/seeds/search GET，根据 name 模糊搜索
Mock.mock(/^\/api\/seeds\/search(\?.*)?$/, 'get', (options) => {
  const url = new URL('http://dummy.com' + options.url);
  const name = url.searchParams.get('name')?.toLowerCase() || '';

  const filtered = sampleData.filter(item =>
    item.name.toLowerCase().includes(name)
  );

  return {
    code: 200,
    data: filtered
  };
});
